package com.eqxiu.ieditor.api.impl.mall;

import com.eqxiu.ieditor.api.mall.IMallServiceApi;
import com.eqxiu.ieditor.dto.Constants;
import com.eqxiu.user.api.dto.User;
import com.google.common.collect.Maps;
import eqxiu.mall.product.dto.HotWordDTO;
import eqxiu.mall.product.dto.ProductFormDTO;
import eqxiu.mall.product.dto.UserDTO;
import eqxiu.mall.product.exception.ApiException;
import eqxiu.mall.product.service.api.AsyncOrderCallbackServiceApi;
import eqxiu.mall.product.service.api.HotWordServiceApi;
import eqxiu.mall.product.service.api.OrderServiceApi;
import eqxiu.mall.product.service.api.ProductServiceApi;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.eqxiu.user.api.UserApi;

import java.util.List;
import java.util.Map;

@Component
public class MallServiceApiImpl implements IMallServiceApi {

    @Autowired
    private OrderServiceApi mallOrderServiceApi;

    @Autowired
    private UserApi userApi;

    @Autowired
    private ProductServiceApi productService;

    @Autowired
    private HotWordServiceApi hotWordServiceApi;

    @Autowired
    private AsyncOrderCallbackServiceApi asyncCallbackServiceApi;

    private final static Integer WAP_CHANNEL = Integer.valueOf(40);

    @Override
    public Map<String, Object> createCallback(String orderType, String orderAmount, final int productId, final String createUser, Integer mallOrderId, boolean useFlag, Integer source) {
        System.out.println("createCallback==========mallOrderId==========="+mallOrderId);
        Map<String, Object> returnMap = Maps.newHashMap();
        Map<String, Object> productLite = productService.getProductLite(productId);
        if (productLite != null && productLite.size() > 0) {
            ProductFormDTO prodFormDTO = new ProductFormDTO();
            // id,type,pageMode,price,orderType
            prodFormDTO.setId(productId);
            prodFormDTO.setType((Integer) productLite.getOrDefault("type", 1));
            prodFormDTO.setPageMode(Integer.valueOf(productLite.getOrDefault("page_mode", "1").toString()));
            prodFormDTO.setPrice(Double.valueOf(orderAmount));
            prodFormDTO.setOrderType(orderType);
            prodFormDTO.setChannel(WAP_CHANNEL);
            // 重复使用
            prodFormDTO.setUseFlag(useFlag);
            User user = userApi.getUser(createUser);
            UserDTO user2 = new UserDTO();
            BeanUtils.copyProperties(user, user2);
            System.out.println("attrGroupId=========="+Integer.parseInt(productLite.get("attrGroupId").toString()));
            returnMap = asyncCallbackServiceApi.callback(prodFormDTO, mallOrderId, Integer.parseInt(productLite.get("attrGroupId").toString()), source, user2);
        }
        return returnMap;
    }

    @Override
    public Map<String, Object> createMallOrder(String orderId, String creater, String orderAmount, String transactionId,
                                               String productName, String channel, final int productId) throws ApiException {
        eqxiu.mall.product.dto.OrderDTO mallOrder = new eqxiu.mall.product.dto.OrderDTO();
        mallOrder.setProductId(productId);
        mallOrder.setOutOrderId(orderId);
        mallOrder.setCreateUser(creater);
        mallOrder.setChannel(channel);
        mallOrder.setName(productName);
        mallOrder.setTotalFee(Double.valueOf(orderAmount).intValue());
        mallOrder.setTransactionId(transactionId);
        return mallOrderServiceApi.saveOrderNew(mallOrder);
    }

    @Override
    public List<HotWordDTO> getMallHotWord(Integer type) {
        List<HotWordDTO> hws = null;
        try {
            hws = hotWordServiceApi.getHotWordsByCategory(type);
        } catch (ApiException e) {
            e.printStackTrace();
            try {// try again
                hws = hotWordServiceApi.getHotWordsByCategory(type);
            } catch (ApiException e1) {
            }
        }
        return hws;
    }
}
